package com.example.blog.controller;

import com.example.blog.entity.User;
import com.yc.upload.vo.Result;
import com.yc.upload.web.UploadAction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorAction {

    @RequestMapping(path="js/ueditor/jsp/controller.jsp",
                    params = "action=config")
    public Map<String,String> config(){
        Map<String,String> ret = new HashMap<>();
        ret.put("imageActionName", "uploadimage");
        ret.put("imageFieldName", "file");
        ret.put("imageUrlPrefix", "");
        return ret;
    }

    @Resource
    UploadAction uploadAction;
    @RequestMapping(path="js/ueditor/jsp/controller.jsp",
            params = "action=uploadimage")
    public Map<String,String> uploadImage(@RequestParam("file") MultipartFile file)
            throws IOException {
        Result upload = uploadAction.upload(file, "ueditor");
        Map<String,String> ret = new HashMap<>();
        ret.put("state","SUCCESS");
        ret.put("url",upload.getData().toString());
        ret.put("title",file.getOriginalFilename());
        ret.put("original",file.getOriginalFilename());
        return ret;
    }
}
